<?php
/**
 * Created by PhpStorm.
 * User: xuechunxuan
 * Date: 2018/10/18
 * Time: 10:58
 */

namespace app\index\controller;

use app\index\model\Subordinate;
use think\Request;

class User extends Session
{
    private function getSubordinate()
    {
        return new Subordinate();
    }

    /*
     * 用户中心
     * */
    public function index(Request $request)
    {
        if ($request->isGet()) {
            return $this->fetch();
        }
    }

    /*
     *下级收益
     * */
    public function lowerIncome(Request $request)
    {
        $page = $request->param("page") ? $request->param("page") : 0;
        $incomeDetail = $this->getSubordinate()->mySelectByKVPage(['get_user_id' => $this->userInfo['id']], $page, 10);
        if ($request->isGet()) {
            $this->assign("incomeDetail", $incomeDetail);
            return $this->fetch();
        } elseif ($request->isPost()) {
            $i = 0;
            foreach ($incomeDetail as $key) {
                $incomeDetail[$i]['add_time'] = date($incomeDetail[$i]['add_time'], "Y-m-d H:i:s");
                $i++;
            }
            return json($incomeDetail, 200);
        }
    }

    /*
     * 我的海报
     * */
    public function banner(Request $request)
    {
        if ($request->isGet()) {
            return $this->fetch();
        }
    }


    /*
     * 下级推荐
     * */
    public function commend(Request $request)
    {
        if($request->isPost()) {
            $savePath = "./static/upload/user/";//保存路径
            $imgPath = $savePath . $this->userInfo['id'] . ".png";//图片名称

            if (!file_exists($imgPath)) {
                $url = input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME')
                    . "/public/index.php/index/index/index/pid/" . $this->userInfo['id'];//二维码生成对应路径
                $this->createQRCode($url, $savePath, $imgPath);
            }
            $url = config("API_HOST") . config("GET_SHARE_CONSUME_1");
            $data = ["posterId" => "1049",
                "qrcodeUrl" => input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME') . "/public/static/upload/user/" . $this->userInfo['id'] . ".png",
                "nickname" => $this->userInfo['nickname'],
                "headimgUrl" => $this->userInfo['head_img_url']
            ];
            $imgPath = $this->http_request($url, false, "post", json_encode($data));
            $this->getUser()->edit(['id'=>\think\Session::get("uid"),'is_spread'=>1]);//标为推广账号
            return $imgPath;
        }elseif($request->isGet()){
            return $this->fetch();
        }
    }

    /*
     * 用户指南
     * */
    public function help()
    {
        return $this->fetch();
    }

}